<?php
/**
 * 功能描述：	表字段管理
 * @datetime	2022-07-11
 * @version   	v2.3.0
 * @author   	ZHL
 * @copyrigh  	2022 ZHL 版权所有
 */

class Admin_FieldController extends Controller {

    // 是否登录
    function __construct($controller, $action) {
        parent::__construct($controller, $action);
        $loginSer = new Admin_LoginServe();
        $loginSer->judgeSessionLogin($controller, $action);
    }

    /**
     * Notes:字段列表
     * User: ZhuHaili
     * Date: 菜单
     */
    function index(){
        $mouldCode = Fun::input('mouldcode');    // 模型编码
        $sortFlag = Fun::input('sortflag');      // 1菜单/2模型
        $tableId = Fun::input('tableid');        // 1主表/2副表
        $fieldSer = new Admin_FieldServe();
        $list = $fieldSer->getFieldList($mouldCode, $sortFlag, $tableId);
        $commM = new Admin_CommonModel();
        $mouldInfo = $commM->getEssayInfo('sysmould', array('mouldcode'=>$mouldCode));
        $this->set('fieldList', $list);
        $this->set('mouldInfo', $mouldInfo);
        $this->set('mouldCode', $mouldCode);
        $this->set('sortFlag', $sortFlag);
        $this->set('tableId', $tableId);
        $this->set('title', '字段列表');
        $temp = $mouldCode ? 'index' : 'classfield';
        $this->display($temp);
    }

    /**
     * Notes:字段添加
     * User: ZHL
     */
    function add(){
        $subFlag = Fun::input('subFlag');
        // 数据提交
        if($subFlag == 'Y') {
            $param = Fun::input();
            foreach($param as $k=>$v){
                $param[$k] = Fun::trimStr($v); // 删除空白字符
            }
            $fieldSer = new Admin_FieldServe();
            $result = $fieldSer->addField($param);
            Fun::json($result, true);
        }
        // 映射页面
        $mouldCode = Fun::input('mouldcode');    // 模型编码
        $sortFlag = Fun::input('sortflag');      // 1菜单/2模型
        $tableId = Fun::input('tableid');        // 1主表/2副表
        $classSer = new Admin_MenuServe();
        $classList = $classSer->getClassList(0);
        $fieldTypeArr = BaseData::$fieldTypeArr;
        $this->set('classList', $classList);
        $this->set('mouldCode', $mouldCode);
        $this->set('sortFlag', $sortFlag);
        $this->set('tableId', $tableId);
        $this->set('fieldTypeArr', $fieldTypeArr);
        $this->set('title', '添加字段');
        $this->display();
	}

    /**
     * Notes:字段修改
     * User: ZHL
     */
    function update(){
        $subFlag = Fun::input('subFlag');
        $fieldSer = new Admin_FieldServe();
        // 数据提交
        if($subFlag == 'Y') {
            $param = Fun::input();
            foreach($param as $k=>$v){
                $param[$k] = Fun::trimStr($v); // 删除空白字符
            }
            $fieldSer = new Admin_FieldServe();
            $result = $fieldSer->updateField($param);
            Fun::json($result, true);
        }
        // 映射页面
        $id = Fun::input('id');
        $info = $fieldSer->getFieldInfo($id);
        $classSer = new Admin_MenuServe();
        $classList = $classSer->getClassList(0);
        $fieldTypeArr = BaseData::$fieldTypeArr;
        $mouldCode = isset($info['mould_code']) ? $info['mould_code'] : '';
        $sortFlag = isset($info['sortflag']) ? $info['sortflag'] : '';
        $tableId = isset($info['tableid']) ? $info['tableid'] : '';
        $this->set('mouldCode', $mouldCode);
        $this->set('sortFlag', $sortFlag);
        $this->set('tableId', $tableId);
        $this->set('info', $info);
        $this->set('classList', $classList);
        $this->set('fieldTypeArr', $fieldTypeArr);
        $this->set('title', '修改字段');
        $this->display();
    }

    /**
     * Notes:字段删除
     * User: ZHL
     */
    function del(){
        $id = Fun::input('id');
        $fieldSer = new Admin_FieldServe();
        $result = $fieldSer->delField($id);
        Fun::json($result, true);
    }

    /**
     * Notes: 排序
     * User: ZHL
     */
    function seq(){
        $param = Fun::input('param');
        $fieldSer = new Admin_FieldServe();
        $result = $fieldSer->seqOrderBy($param);
        Fun::json($result, true);
    }

}